package com.cloudkeeper.leasing.identity.repository;

import com.cloudkeeper.leasing.identity.domain.KPIVillageQuota;
import com.cloudkeeper.leasing.base.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.List;

/**
 * 村考核指标 repository
 * @author yujian
 */
@Repository
public interface KPIVillageQuotaRepository extends BaseRepository<KPIVillageQuota> {

    void deleteAllByParentDistrictIdAndParentQuotaId(String parentDistrictId,String parentQuotaId);

    void deleteAllByTownQuotaId(String townQuotaId);

    void deleteAllByTownQuotaIdIn(List<String> townQuotaIds);

    @Transactional
    @Modifying
    @Query(value = "update dbo.KPI_village_Quota set score = :score, scoreEnd = :scoreEnd where id = :id", nativeQuery = true)
    Integer updateScoreById(@Param("score") String score,@Param("scoreEnd") String scoreEnd,@Param("id") String id);

    @Transactional
    @Modifying
    @Query(value = "update dbo.KPI_village_Quota set score= null,scoreEnd = null where state = '1' and townQuotaId in (:townQuotaId)", nativeQuery = true)
    Integer updateStateByTownQuotaId(@Param("townQuotaId") Collection<String> townQuotaId);
    @Transactional
    @Modifying
    @Query(value = "update dbo.KPI_village_Quota set score= null,scoreEnd = null where parentDistrictId = :parentDistrictId and state = '1' and townQuotaId in (:townQuotaId) ", nativeQuery = true)
    Integer updateStateByTownQuotaIdAndParentDistrictId(@Param("townQuotaId") Collection<String> townQuotaId,@Param("parentDistrictId") String parentDistrictId);

    List<KPIVillageQuota> findAllByTownQuotaIdAndQuarterOrderByDistrictId(@Nonnull String townQuotaId, @Nonnull String quarter);
    List<KPIVillageQuota> findAllByTownQuotaIdAndQuarterAndParentDistrictIdOrderByDistrictId(@Nonnull String townQuotaId, @Nonnull String quarter,@Nonnull String parentDistrictId);
    List<KPIVillageQuota> findAllByTownQuotaIdAndQuarterAndDistrictIdOrderByDistrictId(@Nonnull String townQuotaId, @Nonnull String quarter,@Nonnull String districtId);
    List<KPIVillageQuota> findAllByParentQuotaId(String parentQuotaId);

    @Transactional
    @Modifying
    @Query(value = "update dbo.KPI_village_Quota set state='0' where parentDistrictId = :parentDistrictId and parentQuotaId like :year + '%' ", nativeQuery = true)
    Integer updateRejectListStatus(@Param("parentDistrictId") String parentDistrictId, @Param("year") String year);

    @Transactional
    @Modifying
    @Query(value = "update dbo.KPI_village_Quota set state='0' where townQuotaId in (:townQuotaId) ", nativeQuery = true)
    Integer updateRejectListStatusDepartment(@Param("townQuotaId") Collection<String> townQuotaId);

    @Transactional
    @Modifying
    @Query(value = "update dbo.KPI_village_Quota set score=null, scoreEnd=null where parentDistrictId = :parentDistrictId and parentQuotaId like :year + '%' and state = 1", nativeQuery = true)
    Integer updateScore(@Param("parentDistrictId") String parentDistrictId, @Param("year") String year);
}
